
package dao;

import business.Reserva;
import java.sql.Connection;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import vo.ReservaVO;

        
    
public class ReservaDAO {
 
    public static List<ReservaVO> getReservas() {

        Connection con = ConnectionFactory.getConnection();

        List<ReservaVO> lista = new ArrayList<>();

        try {

            String query = "SELECT * FROM Reserva";
            PreparedStatement stmt = con.prepareStatement(query);
            ResultSet res = stmt.executeQuery();

            while (res.next()) {
           
                String nomeCategoria = res.getString(2);
                Date dataRetiradaEfetivo = res.getDate(3);
                Date dataDevolucaoEfetivo = res.getDate(4);
                float orcamento = res.getFloat(5);
                String cpfCliente = res.getString(6);               
                String cpfFuncionario = res.getString(7);
                
                
                lista.add(new ReservaVO(nomeCategoria,dataRetiradaEfetivo,dataDevolucaoEfetivo,orcamento,cpfCliente,cpfFuncionario));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lista;
    }
    public static List<ReservaVO> getReservaPeloID(String ID) {

        Connection con = ConnectionFactory.getConnection();
        int id_aux = Integer.parseInt(ID);
        List<ReservaVO> lista = new ArrayList<>();

        try {

            String query = "SELECT * FROM Reserva where ID=?";
   
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setInt(1,id_aux);           
            ResultSet res = stmt.executeQuery();

            while (res.next()) {
           
                String nomeCategoria = res.getString(4);
                Date dataRetiradaEfetivo = res.getDate(5);
                Date dataDevolucaoEfetivo = res.getDate(6);
                float orcamento = res.getFloat(7);
                String cpfCliente = res.getString(2);               
                String cpfFuncionario = res.getString(3);
                
                
                lista.add(new ReservaVO(nomeCategoria,dataRetiradaEfetivo,dataDevolucaoEfetivo,orcamento,cpfCliente,cpfFuncionario));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lista;
    }
    public static void insertReserva(ReservaVO reserva) {
         Connection con = ConnectionFactory.getConnection();
        
        try {

            String query = "INSERT INTO reserva (nome_categoria,data_retirada_acordada,data_devolucao_Acordada,orcamento_preliminar,cpf_cliente,cpf_funcionario)  VALUES(?,?,?,?,?,?)";
            PreparedStatement stmt = con.prepareStatement(query);

            stmt.setString(1, reserva.getnomeCategoria());
            stmt.setDate(2,reserva.getdataRetirada());
            stmt.setDate(3,reserva.getdataDevolucao());
            stmt.setFloat(4, reserva.getOrcamentoPreliminar());
            stmt.setString(5,reserva.getCpfCliente());
            stmt.setString(6,reserva.getCpfFuncionario());
            
            stmt.executeUpdate();

           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void deleteReservaPeloID(String ID){
        
        Connection con=ConnectionFactory.getConnection();
        int id_aux = Integer.parseInt(ID);
        try{
            
            String query="Delete from Reserva where ID=?";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setInt(1,id_aux);           

       
            stmt.executeUpdate();

        }catch(Exception e){
           
            e.printStackTrace();
        }
        
    }
    public static boolean ReservaExiste(String ID)
    {
        Connection con = ConnectionFactory.getConnection();

        int id_aux = Integer.parseInt(ID);
        try{
            String query = "Select * from Reserva where ID=?";
            PreparedStatement stmt= con.prepareStatement(query);
            int aux;
            stmt.setInt(1,id_aux);
 
            ResultSet res = stmt.executeQuery();
            while(res.next()){
            aux=res.getInt(1);
            
            if(aux==id_aux)
            {                   
            return true;
            }}

        }catch(Exception e){

        }  
        System.out.println("false");
       return false;       
    }
    
    public static boolean DataReservaValida(String ID)
    {
        Connection con = ConnectionFactory.getConnection();

        int id_aux = Integer.parseInt(ID);
        try{
            String query = "Select * from Reserva where ID=?";
            PreparedStatement stmt= con.prepareStatement(query);
            int aux;
            stmt.setInt(1,id_aux);
 
            ResultSet res = stmt.executeQuery();
            while(res.next()){
                
            Date data = new Date(System.currentTimeMillis());
            SimpleDateFormat converte= new SimpleDateFormat("dd-MM-yyyy");
            String dataAtual = converte.format(data);
            
            System.out.println(dataAtual);
           
            aux=res.getInt(1);
            Date dataRetiradaEfetivo = res.getDate(5);
            String dataRetirada = converte.format(dataRetiradaEfetivo);
            System.out.println(dataRetirada);
           
            int DiasRet,DiasAtual,MesRet,MesAtual,AnoRet,AnoAtual;
            
            DiasRet=Reserva.GeraDias(dataRetirada);
            DiasAtual=Reserva.GeraDias(dataAtual);
            MesRet=Reserva.GeraMes(dataRetirada);
            MesAtual=Reserva.GeraMes(dataAtual);
            AnoRet=Reserva.GeraAno(dataRetirada);            
            AnoAtual=Reserva.GeraAno(dataAtual);   
            
            if((AnoAtual==AnoRet)&&(DiasRet==DiasAtual)&&(MesAtual==MesRet))
            {
                return true;
            }
            return false;
            }
  
            
  

        }catch(Exception e){

        }  
       // System.out.println("false");
       return false;        
    }
   
    
}
